/*
 * Copyright 2018-2019 KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wanchuang.hepos.ui.page.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.wanchuang.architecture.utils.GsonUtils;
import com.wanchuang.architecture.utils.Utils;
import com.wanchuang.architecture.utils.ValidateUtils;
import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.request.LoginRequestViewModel;
import com.wanchuang.hepos.bridge.state.home.BindXinCardViewModel;
import com.wanchuang.hepos.databinding.FragmentBindXinCardBinding;
import com.wanchuang.hepos.databinding.FragmentMeNetworkFiveBinding;
import com.wanchuang.hepos.ui.base.BaseFragment;
import com.wanchuang.hepos.ui.dialog.RecycleViewDialog;
import com.wanchuang.hepos.ui.dialog.SelectMonthDialog;

import kale.ui.view.dialog.EasyDialog;


/**
 * Create by KunMinX at 19/10/29
 */
public class BindXinCardFragment extends BaseFragment {

    private FragmentBindXinCardBinding mBinding;
    private BindXinCardViewModel mViewModel;
    private LoginRequestViewModel mRequestViewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(BindXinCardViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(LoginRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bind_xin_card, container, false);
        mBinding = FragmentBindXinCardBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, error -> {
            hideProgress();
            showShortToast(error);
        });

        mRequestViewModel.getResultString().observe(this, result -> {
            hideProgress();
            nav().navigateUp();
        });

        mRequestViewModel.getBanks().observe(this, banks -> mViewModel.banks.addAll(banks));

        mRequestViewModel.getCredit_card().observe(this, credit_card -> {
            hideProgress();
            mViewModel.orderId = credit_card.getOrderId();
            mViewModel.startCode.set(true);
        });
        mRequestViewModel.get_support("2", "1");  // 类型:1-结算卡;2-信用卡   // VIP支持：1-支持;2-所有


    }


    public class ClickProxy {
        public void submit() {
            String name = mViewModel.name.get();
            if (TextUtils.isEmpty(name)) {
                showShortToast("请输入姓名");
                return;
            }
            String card = mViewModel.cardNumber.get();
            if (TextUtils.isEmpty(card)) {
                showShortToast("请填写您本人的信用卡卡号");
                return;
            }

            String valid_time = mViewModel.cardTime.get();
            if (TextUtils.isEmpty(valid_time)) {
                showShortToast("请选择该卡有效期年月");
                return;
            }

            String cvv = mViewModel.cardSafeCode.get();
            if (TextUtils.isEmpty(cvv)) {
                showShortToast("请输入该卡安全码");
                return;
            }
            String mobile = mViewModel.phone.get();
            if (TextUtils.isEmpty(mobile)) {
                showShortToast("请输入手机号码");
                return;
            }
            if (mobile.length() != 11) {
                showShortToast("请输入11位手机号码");
                return;
            }
            String captcha = mViewModel.captcha.get();
            if (TextUtils.isEmpty(captcha)) {
                showShortToast("请输入手机验证码");
                return;
            }
            showProgress();
            // 信用卡入网
            mRequestViewModel.creditCard(name,card,valid_time,cvv,mobile,mViewModel.orderId,mViewModel.captcha.get());
        }

        public void back() {
           nav().navigateUp();
        }

        public void getCode() {
            String name = mViewModel.name.get();
            if (TextUtils.isEmpty(name)) {
                showShortToast("请输入姓名");
                return;
            }
            String card = mViewModel.cardNumber.get();
            if (TextUtils.isEmpty(card)) {
                showShortToast("请填写您本人的信用卡卡号");
                return;
            }

            String valid_time = mViewModel.cardTime.get();
            if (TextUtils.isEmpty(valid_time)) {
                showShortToast("请选择该卡有效期年月");
                return;
            }

            String cvv = mViewModel.cardSafeCode.get();
            if (TextUtils.isEmpty(cvv)) {
                showShortToast("请输入该卡安全码");
                return;
            }
            String mobile = mViewModel.phone.get();
            if (TextUtils.isEmpty(mobile)) {
                showShortToast("请输入手机号码");
                return;
            }
            if (mobile.length() != 11) {
                showShortToast("请输入11位手机号码");
                return;
            }
            showProgress();
            mRequestViewModel.credit_send(name,card,valid_time,cvv,mobile);
        }


        public void selectTime() {
            SelectMonthDialog.Builder builder = new SelectMonthDialog.Builder(mActivity);
            builder.setOnDialogClickListener((view, month) -> {
                mViewModel.cardTime.set(month);
            });
            EasyDialog build = builder.build();
            build.setCancelable(true);
            build.show(getChildFragmentManager());
        }

        public void showBank() {
            if (ValidateUtils.isValidate(mViewModel.banks)) {
                RecycleViewDialog.Builder builder = new RecycleViewDialog.Builder(mActivity);
                builder.setData(GsonUtils.serializedToJson(mViewModel.banks)).setTitle("VIP收款支持银行");
                EasyDialog build = builder.build();
                build.show(getChildFragmentManager());
            } else {
                showShortToast("暂无支持银行");
            }
        }
    }


    @Override
    public void onStop() {
        if (Utils.isAppForeground()||!mSharedViewModel.isLoading.getValue()) {
            mViewModel.startCode.set(false);
        }
        super.onStop();
    }
}
